package groovy

import redis.clients.jedis.Jedis
import com.aliyun.dingtalkoauth2_1_0.Client
import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest
import com.aliyun.teaopenapi.models.Config


static String getTokenFromDingTalk() {
    Config config = new Config().setProtocol("https").setRegionId("central");
    var authClient= new Client(config)
    var  accessTokenConfig=new GetAccessTokenRequest()
            .setAppKey("dingvnclcjl37jgrgok1")
            .setAppSecret("ttfStLJUDvYyvzfFIV-QYkVm5l33sEm12G065aiHrb-sjWYRNFtFHdVONRr0uRFU");
    return authClient.getAccessToken(accessTokenConfig).getBody().getAccessToken();
}
static String getTokenFromRedis(){
    def jedis = new Jedis("localhost", 6379)
    def value = jedis.get("dingToken")
    if (value!=null){
        return value.replace("\"","")
    }
    else{
        def token=getTokenFromDingTalk()
        jedis.setex("dingToken",7000,token)
        return token
    }
}